var winston = require('winston');

module.exports = function(pool, req, res, next){
	var action = req.params.action;
	var photoId = req.query.photoId;
	var eventId, state;
	if(!photoId){
		eventId = req.query.eventId;
	}
	state = action === 'hide' ? 1 : 0;
	// update the state of a single photo by photo_id
	if(photoId){
		var queryString = 'UPDATE photo_list SET is_hidden=' + state + ' WHERE photo_id=' + photoId;
		pool.getConnection(function(err, connection){
			if(err){
				winston.error('DB connection error: ' + err);
				res.status(500).send('DB connection error: ' + err);
			}
			connection.query(queryString, function(err, rows){
				if(err){
					winston.error('DB query error: ' + err);
					res.status(500).send('DB query error: ' + err);
				}else{
					res.send({msg: 'ok'});
				}
				connection.release();
			});
		});
	}
	// update the state of all the photos for an event
	else if(eventId){
		var queryString = 'UPDATE photo_list SET is_hidden=' + state + ' WHERE event_id=' + eventId;
		pool.getConnection(function(err, connection){
			if(err){
				winston.error('DB connection error: ' + err);
				res.status(500).send('DB connection error: ' + err);
			}
			connection.query(queryString, function(err, rows){
				if(err){
					winston.error('DB query error: ' + err);
					res.status(500).send('DB query error: ' + err);
				}else{
					res.send({msg: 'ok'});
				}
				connection.release();
			});
		});
	}
	else{
		winston.error('ROUT error, parameters not match');
		res.status(500).send('ROUT error, parameters not match')
	}
	
}